<template>
  <div id="app" style="overflow: hidden">
    <!--   搜索 searchBar-->
    <div
        class="flex"
        style="
          justify-content: space-between;
          position: relative;
          align-items: center;
          flex-wrap: wrap;
        "
    >
      <div class="flex" style="margin-right: 20px">
        <div class="search">
          店铺编码：
          <el-input
              clearable
              placeholder="请输入"
              style="width: 200px"
              size="small"
              prefix-icon="el-icon-search"
              v-model="params.storeCode"
          >
          </el-input>
        </div>
        <div class="search">
          店铺名称：
          <el-input
              clearable
              placeholder="请输入"
              style="width: 200px"
              size="small"
              prefix-icon="el-icon-search"
              v-model="params.storeName"
          >
          </el-input>
        </div>
        <div class="search">
          行政区域：
          <el-cascader
              size="small"
              v-model="selectedAddress"
              :options="addressList"
              @change="getAddressData"
              :props="{
                checkStrictly: true,
                label: 'name',
                value: 'code',
                children: 'children',
              }"
              clearable
              filterable
              placeholder="请选择省市区"
              style="width: 200px"
          />
        </div>
        <div class="search">
          加盟商姓名：
          <el-input
              clearable
              placeholder="请输入"
              style="width: 200px"
              size="small"
              prefix-icon="el-icon-search"
              v-model="params.nickName"
          >
          </el-input>
        </div>
        <div class="search">
          店铺品种：
          <el-select
              v-model="params.storeVariety"
              placeholder="请选择"
              style="width: 200px"
              size="small"
              clearable
          >
            <el-option label="零食店" value="1"></el-option>
            <el-option label="便利店" value="2"></el-option>
          </el-select>
        </div>
        <div class="search">
          店铺类型：
          <el-select
              v-model="params.storeType"
              placeholder="请选择"
              style="width: 200px"
              size="small"
              clearable
          >
            <el-option label="加盟店" value="F"></el-option>
            <el-option label="直营店" value="R"></el-option>
          </el-select>
        </div>
        <div class="search">
          <el-button
              type="primary"
              size="small"
              @click="_search"
              icon="el-icon-search"
          >查询
          </el-button>
          <el-button
              size="small"
              type="primary"
              @click="_reset"
              icon="el-icon-refresh"
          >重置
          </el-button>
        </div>
        <!--    新增按钮 -->
        <!--        <div style="position: absolute;right:0;bottom: 10px">-->
        <!--          <el-button style="background-color: #303e9d;color: #eeeeee" size="small" @click="addStore"-->
        <!--                     v-if="authorityList.includes(301)">新增店铺-->
        <!--          </el-button>-->
        <!--        </div>-->
      </div>
    </div>
    <div style="margin-bottom: 10px">
      <el-button
          type="primary"
          size="small"
          @click="addStore"
          v-if="authorityList.includes(1801)"
      ><i class="el-icon-plus" style="margin-right: 5px"></i>新 增
      </el-button>
    </div>
    <!--    tableBar-->
    <el-table
        :data="storeList"
        overflow="auto"
        border
        stripe
        height="calc(100vh - 320px)"
        :cell-style="{ textAlign: 'center' }"
        :header-cell-style="{
          fontSize: '14px',

          'background-color': '#F3F6FD',
          color: '#333333',
          textAlign: 'center',
        }"
    >
      <el-table-column type="index" width="50" label="序号"></el-table-column>
      <el-table-column
          prop="storeCode"
          label="门店编码"
          width="150px"
          show-overflow-tooltip
      ></el-table-column>
      <el-table-column
          prop="brandPartyName"
          label="所属品牌"
      ></el-table-column>
      <el-table-column
          prop="storeName"
          label="门店名称"
          width="120px"
          show-overflow-tooltip
      ></el-table-column>
      <el-table-column
          prop="address"
          label="门店地址"
          width="250px"
          show-overflow-tooltip
      ></el-table-column>
      <el-table-column prop="storeManagerPhone" label="店长电话" width="150px"></el-table-column>
      <el-table-column
          prop="storeStatus"
          label="门店状态"
          show-overflow-tooltip
      >
        <template v-slot:default="scope">
          <span>{{ typeMap[scope.row.storeStatus] || '' }}</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="type"
          label="装修类型"
          show-overflow-tooltip
      >
        <template v-slot:default="scope">
          <span>{{ scope.row.type === 1 ? '新店装修' : scope.row.type === 2 ? '老店升级' : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="storeArea" label="门店面积">
        <template v-slot:default="scope">
          <span>{{ scope.row.storeArea }}㎡</span>
        </template>
      </el-table-column>
      <el-table-column prop="nickName" label="加盟商姓名" width="150px"></el-table-column>
      <el-table-column prop="phoneNum" label="加盟商电话" width="150px"></el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间"
          width="110px"
          show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="门店类型">
        <template slot-scope="scope">
          <span v-if="scope.row.storeType == 'F'">加盟店</span>
          <span v-if="scope.row.storeType == 'R'">直营店</span>
        </template>
      </el-table-column>
      <el-table-column label="门店品种">
        <template slot-scope="scope">
          <span v-if="scope.row.storeVariety == 1">零食店</span>
          <span v-if="scope.row.storeVariety == 2">便利店</span>
        </template>
      </el-table-column>
      <el-table-column label="门店规模">
        <template slot-scope="scope">
          <span v-if="scope.row.storeScale == 'S'">小型</span>
          <span v-if="scope.row.storeScale == 'M'">中型</span>
          <span v-if="scope.row.storeScale == 'L'">大型</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-link
              style="color: #303e9d; margin-left: 10px"
              @click="editStore(scope.row)"
              v-if="authorityList.includes(1802)"
          >编辑
          </el-link>
          <el-link
              style="color: #303e9d; margin-left: 10px"
              @click="deleteStore(scope.row)"
              v-if="authorityList.includes(1803)"
          >删除
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div style="margin-top: 20px; text-align: right">
      <el-pagination
          align="center"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNumber"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount"
      >
      </el-pagination>
    </div>
    <add_store-manage
        ref="addStore"
        @addStatus="receiveMessage"
    ></add_store-manage>
  </div>
</template>

<script>
import {mapState} from 'vuex'
import dayjs from 'dayjs'
import add_storeManage from './componentDialog/add_storeManage.vue'
import addressList from '../../assets/js/address.js'

export default {
  name: 'store',
  meta: {
    title: '店铺管理',
    icon: 'el-icon-s-shop',
    authorityList:1800,
    sort: 4,
  },
  components: {
    add_storeManage,
  },
  data() {
    return {
      createTime: '',
      maxHeight: '',
      storeDialog: false,
      detailsDialog: false,
      dialogTitle: '',
      storeList: [],
      areaList: [],
      cascaderProps: {
        value: 'code',
        label: 'name',
        children: 'children',
        emitPath: true,
      },
      addressList: addressList,
      selectedAddress: '',
      authorityList: [],
      totalCount: 0,
      storeDetails: {},
      storeForm: {
        storeName: '',
        address: '',
        areaId: '',
        addUserName: '',
        addUserPhone: '',
        storeType: 1,
      },
      typeMap: {
        ...{
          1: '待进场',
          2: '施工中',
          3: '验收中',
          4: '待开业',
          5: '已开业'
        }
      },
      params: {
        pageSize: 20,
        areaCode: '',
        parentCode: '',
        pageNumber: 1,
        storeName: '',
        storeCode: '',
        nickName: '',
        storeVariety: '',
        storeType: '',
      },
    }
  },
  computed: {
    ...
        mapState(['userData']),
  },
  mounted() {
    this.$nextTick(() => {
      this.maxHeight = window.innerHeight - 350
    })
    window.onresize = () => {
      this.$nextTick(() => {
        this.maxHeight = window.innerHeight - 350
      })
    }
  }
  ,
  created() {
    this.getStoreList()
    // this.getAreaLis t();
    const {authorityList = []} =
    this.$quinn.getStorage('userDatav3') || this.$store.state.userData || {}
    this.authorityList = authorityList
  }
  ,
  methods: {
    _search() {
      this.params.pageNumber = 1
      this.getStoreList()
    }
    ,
    getAddressData(val) {
      if (Array.isArray(val) && val.length > 0) {
        this.params.parentCode = val[0] || ''
        this.params.areaCode = val[1] || ''
      } else {
        this.params.parentCode = ''
        this.params.areaCode = ''
      }
    }
    ,
    getStoreList() {
      this.$api.storeGetStoreVo(this.params).then((res) => {
        this.storeList = res.data || []
        this.totalCount = res.total || 0
      })
    }
    ,
    _reset() {
      this.params = this.$options.data.call(this).params
      this.getStoreList()
    }
    ,
    getAreaList() {
      // this.$api.assignGetAreaList().then(res => {
      //   this.areaList = res || [];
      // });
    }
    ,
    getDate(val) {
      if (val) {
        this.params.startTime = dayjs(val[0]).format('YYYY-MM-DD')
        this.params.endTime = dayjs(val[1]).format('YYYY-MM-DD')
      } else {
        this.params.startTime = ''
        this.params.endTime = ''
      }
    }
    ,
    handleSizeChange(val) {
      this.params.pageSize = val
      this.getStoreList()
    }
    ,
    handleCurrentChange(val) {
      this.params.pageNumber = val
      this.getStoreList()
    }
    ,
    addStore() {
      this.$refs.addStore.handleOpen()
    }
    ,
    editStore(row) {
      this.dialogTitle = '编辑店铺'
      this.storeForm = JSON.parse(JSON.stringify(row))
      this.$refs.addStore.handleOpen(this.storeForm)
    }
    ,
    viewDetails(row) {
      this.storeDetails = row
      this.detailsDialog = true
    }
    ,
    deleteStore(row) {
      this.$confirm('确定要删除该店铺吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
          .then(() => {
            this.$api.storeDelStoreById({storeId: row.storeId}).then((res) => {
              this.$message.success('删除成功')
              this.getStoreList()
            })
          })
          .catch(() => {
          })
    }
    ,
    receiveMessage(val) {
      if (val) {
        this.getStoreList()
      }
    }
    ,
  }
  ,
}
</script>

<style scoped>
@import '../../assets/css/public.css';

.search {
  margin: 0 20px 10px 0;
  display: flex;
  align-items: center;
}

.flex {
  display: flex;
  flex-wrap: wrap;
}

/deep/ .el-form-item__error {
  left: 80px;
}
</style>
